/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import hibernateut.HibernateUtil;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

/**
 *
 * @author clsma
 */
public abstract class Modelo {

    public Modelo() {
        Session session = HibernateUtil.getSessionFactory().openSession();
    }

    static String sqlCons = "";

    public static void setSql(String sql) {
        sqlCons = sql;
    }

    public static List listar(String sql) {
        setSql(sql);
        List res = new LinkedList() ;
        Hashtable auxHashtable = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        SQLQuery sqlQuery = session.createSQLQuery(sql);
        sqlQuery.setResultTransformer(AliasToEntityOrderedMapResultTransformer.INSTANCE);
        List<Map<String, Object>> listMapRs = sqlQuery.list();

        if (listMapRs != null && listMapRs.size() > 0) {
            for (Map<String, Object> registro : listMapRs) {
                auxHashtable = new Hashtable();
                for (Map.Entry<String, Object> campo : registro.entrySet()) {
                    String key = campo.getKey();
                    Object value = campo.getValue();
                    auxHashtable.put(key, value.toString().trim());
                } 
                res.add(auxHashtable);
            }
          
        }

        return res;

    }
}
